"""
 -*- coding: utf-8-*-
 @File: api_home.py
 @Author: chen
 @time: 2023/12/01/ 22:01
"""
import jsonpath

from stage_L1.lim_project.api_po.businesses.api_base import BaseApi
from stage_L1.lim_project.api_po.businesses.api_login import LoginApi


class HomeApi(BaseApi):
    def __init__(self):
        LoginApi().login()

    def index_statistics(self):
        """获取首页静态信息接口"""
        path = "/project/get-index-statistics"
        resp = self.send("GET", path)
        return resp.json()

    def envir_view(self):
        """获取环境信息接口"""
        path = "/conf/envir-view"
        resp = self.send("GET", path)
        return resp.json()

    def param_type(self):
        """获取参数类型接口"""
        path = "/conf/param-type"
        resp = self.send("GET", path)
        return resp.json()

    def project_view(self, page=None, page_size=None):
        """获取项目列表接口"""
        # logger.info("获取项目列表")
        path = "/project/project-view"
        data = f"page={page}&page_size={page_size}"
        self.payload = {"params": data}
        resp = self.send("GET", path, params=data)
        return resp.json()

    def get_all_projects(self):
        result = self.project_view()
        expr = "$..results"
        projects = jsonpath.jsonpath(result, expr)
        # logger.info("获取所有项目信息")
        return projects[0]

    def get_project_ids(self):

        result = self.get_all_projects()
        expr = "$..id"
        ids = jsonpath.jsonpath(result, expr)
        # logger.info("获取所有项目id")
        return ids

    def get_project_names(self):
        result = self.project_view()
        expr = """$..name"""
        project_names = jsonpath.jsonpath(result, expr)
        # logger.info("获取所有项目名称")
        return project_names

    def get_project_info_by_name(self, project_name):
        result = self.get_all_projects()
        expr = f"""$..[?(@.name=="{project_name}")]"""
        project_info = jsonpath.jsonpath(result, expr)
        # logger.info(f"获取项目{project_name}的信息")
        return project_info[0]

    def _get_project_id_by_name01(self, project_name):
        result = self.project_view()
        # print(result)
        expr = fr"""$..[?(@.name=='{project_name}')]..id"""
        # print(expr)
        project_id = jsonpath.jsonpath(result, expr)
        if project_id:
            return project_id[0]
        else:
            return None

    def _get_project_id_by_name02(self, project_name):
        result = self.project_view().get("results")
        for i in result:
            # print(i.get("name"),project_name)
            if i.get("name") == project_name:
                return i.get("id")
        else:
            return None

    def get_project_id_by_name(self, project_name):
        if project_name in ["@", ";", "\"", "'", "\\", ")"]:
            return self._get_project_id_by_name02(project_name)
        else:
            return self._get_project_id_by_name01(project_name)


if __name__ == '__main__':
    h0 = HomeApi()
    # print(h0.get_project_id_by_name("测试1112"))
    print(h0.get_project_id_by_name("@"))
    print(h0.get_project_id_by_name("\\"))
    print(h0.get_project_id_by_name(";"))
    print(h0.get_project_id_by_name("\'"))
    print(h0.get_project_id_by_name("\""))
    print(h0.get_project_id_by_name(")"))
    print(h0.get_project_id_by_name("云系统"))
